<template>
  <div>
    <el-upload

      class="avatar-uploader"
      action="#"
      :http-request="onRequest"
      list-type="picture-card"
      :file-list="fileList"
      :on-preview="onPreview"
      :on-remove="onRemove"
      accept="image/gif,image/png"
      :before-upload="beforeUpload"
      :on-change="onChange"
      :limit="limit"
      :on-exceed="onExceed"
    >

      <i class="el-icon-plus avatar-uploader-icon" />

    </el-upload>
    <el-progress v-if="showPercent" style="circle" :percentage="percentage" />
    <el-dialog
      title="头像"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <img :src="imgUrl">
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDoRnsgFXYJn1zh8JCo9D0WlkSt3xaw1lG',
  SecretKey: 'GFwOYR3PUIp55oHTP4SaS3NPAgvta1C6'
})
console.log(cos)
export default {
  props: {
    limit: {
      type: Number,
      default: () => 0
    }
  },
  data() {
    return {
      fileList: [

      ],
      dialogVisible: false,
      imgUrl: '',
      showPercent: false,
      percentage: 0
    }
  },

  created() {

  },

  methods: {
    onRequest(data) {
      const key = data.file.name
      cos.putObject(
        {
          Bucket: 'thhhht-1308960748',
          Region: 'ap-shanghai',
          Key: key,
          StorageClass: 'STANDARD',
          Body: data.file,
          onProgress: progressData => {
            if (progressData.percent === 1) {
              this.percentage = 100
              this.showPercent = false
            }
          }
        }, (err, data) => {
          console.log(err || data)
          if (!err && data.statusCode === 200) {
            const lastUrl = this.fileList.length - 1
            this.fileList[lastUrl].url = 'https://' + data.Location
            // 图片上传成功后，触发onsuccess事件 将filelist数据传给父页面
            this.$emit('on-success', this.fileList)
          }
        })
    },
    // 点击预览
    onPreview(file) {
      this.imgUrl = file.url
      this.dialogVisible = true
    },
    // 点击删除 组件fileList已经为0 只需要赋值给当前数组
    onRemove(file, fileList) {
      this.fileList = fileList
    },
    // 图片上传之前还要进行判断
    beforeUpload(file) {
      // 判断图片格式
      const imgType = ['image/gif', 'image/png']
      if (!imgType.includes(file.type)) {
        this.$message.error('亲，请上传正确格式的图片')
        return false
      }
      // 判断图片大小
      const maxSize = 10 * 1024 * 1024
      if (file.size > maxSize) {
        this.$message.error('请上传1MB内图片')
        return false
      }
    },
    // 图片上传本地被保存本地数据
    onChange(file, fileList) {
      this.fileList = fileList
    },
    onExceed() {
      this.$$message.error(this.limit + '张')
    }
  }
}
</script>

<style  lang='scss'>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 148px;
    height: 148px;
    line-height: 148px;
    text-align: center;
  }
  // .avatar {
  //   width: 178px;
  //   height: 178px;
  //   display: block;
  // }
</style>
